package com.china.book.api;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wangxiangyun on 2018/8/30.
 */
@RestController
@Slf4j
public class BookController {
    @GetMapping("/book/classical")
    public JSONObject getBook() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JSONObject bookJson = new JSONObject();
        bookJson.put("1", "西游记9999888");
        bookJson.put("2", "水浒传0000");
        bookJson.put("3", "红楼梦1111");
        bookJson.put("4", "三国演义222");
        log.info("{}: 访问了书店, bookJson:{}, authentication:{}", authentication.getName(), bookJson, authentication);
        return bookJson;
    }

    @PostMapping("/book/sell")
    public JSONObject sellBook() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JSONObject bookJson = new JSONObject();
        bookJson.put("4", "三国演义");
        log.info("{}: 买了一本书, bookJson:{}, authentication:{}", authentication.getName(), bookJson, authentication);
        return bookJson;
    }
}
